<template>
  <card ref="Card">
    <div slot="header">
      <el-input placeholder="请输入表名" v-model="searchTableName" clearable class="w-200" @keyup.enter.native="getData"/>
      <el-button type="success" class="el-icon-search ml-5" @click="getData">搜索</el-button>
    </div>
    <el-table :data="formData">
      <el-table-column prop="tableName" label="表名"/>
      <el-table-column label="创建时间" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.createTime | formatDateTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="engine" label="数据库引擎"/>
      <el-table-column prop="coding" label="编码集"/>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
              icon="el-icon-check"
              size="mini"
              style="margin-left: -1px;margin-right: 2px"
              type="primary"
              @click="jump(scope.row.tableName)"
          >配置
          </el-button>
          <el-button
              :loading="columnLoading"
              icon="el-icon-check"
              size="mini"
              style="margin-left: -1px;margin-right: 2px"
              type="primary"
              @click="toGen(scope.row.tableName)"
          >生成
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination ref="Pagination" @update="getData"></pagination>
  </card>
</template>

<script>
import {generator, pageTables} from "@/api/tools/generator";

export default {
  name: "Generator",
  data() {
    return {
      columnLoading: false,
      searchTableName: null,
      formData: [],
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      let pagination = this.$refs.Pagination;
      let param = {
        current: pagination.current,
        size: pagination.size,
        tableName: this.searchTableName,
      };
      pageTables(param).then(result => {
        let response = result.resultParam.tableInfoDtoPage;
        this.formData = response.records;
        pagination.total = response.total;
      }).catch(() => {
        this.$refs.Card.stop();
      })
    },
    jump(name){
      this.$storeSet('setBreadcrumb', ['代码生成配置']);
      this.$storeSet('addTags', {title: '代码生成配置', name: 'generator_config', indexPath: ['代码生成配置']});
      this.$storeSet('setActive', '代码生成配置');
      this.$router.push({
        name: 'generator_config',
        query:{key:name}
      })
    },
    toGen(tableName){
      this.columnLoading = true
      let param = {
        tableName: tableName
      };
      generator(param).then(() => {
        this.columnLoading = false
      }).catch(() => {
        this.columnLoading = false
        this.$refs.Card.stop();
      })
    },
  }
}
</script>